package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper,AdSensitive> implements AdSensitiveService {
    /**
     * 敏感词查询
     * @param sensitiveDTO
     * @return
     */
    @Override
    public ResponseResult queryList(SensitiveDTO sensitiveDTO) {
        //1.参数检验
        if (sensitiveDTO==null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"参数为空");
        }
        sensitiveDTO.checkParam();
        //2.进行分页查询
        Page<AdSensitive> sensitivePage = new Page<>(sensitiveDTO.getPage(), sensitiveDTO.getSize());
        //3.其他查询
        LambdaQueryWrapper<AdSensitive> lambdaQuery = Wrappers.<AdSensitive>lambdaQuery();
        //4.根据名称进行查询
        if (StringUtils.isNotBlank(sensitiveDTO.getName())){
            lambdaQuery.like(AdSensitive::getSensitives,sensitiveDTO.getName());
        }
        //5.封装结果
        IPage<AdSensitive> page = this.page(sensitivePage, lambdaQuery);
        //6.返回结果
        PageResponseResult responseResult = new PageResponseResult(sensitiveDTO.getPage(), sensitiveDTO.getSize(), page.getTotal(), page.getRecords());

        return responseResult;
    }

    /**
     * 敏感词添加
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1.参数检验
        if (adSensitive==null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"参数为空");
        }
        //2.敏感词不能为空
        if (StringUtils.isBlank(adSensitive.getSensitives())){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"敏感词不能为空");
        }
        //3.敏感词长度不能超过10
        if (adSensitive.getSensitives().length()>10){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"敏感词长度不能超过10");
        }
        //4.敏感词不能重复
        LambdaQueryWrapper<AdSensitive> eq = Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives());
        int count = this.count(eq);
        if (count>0){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST,"敏感词已经存在");
        }
        //新增时间
        adSensitive.setCreatedTime(new Date());
        //保存
        save(adSensitive);
        //5.返回查询的结果
        return ResponseResult.okResult();
    }

    /**
     * 敏感词修改
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult updateName(AdSensitive adSensitive) {
        //1.参数校验
        if (adSensitive.getId()==null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"参数为空");
        }
        //2。获取到原来敏感词id
        AdSensitive oldSens = this.getById(adSensitive);
        if (oldSens==null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"敏感词id不存在");
        }
        //3.执行修改操作
        //获取到敏感词
        String sensitives = adSensitive.getSensitives();
        //3.1不能为空且不能空白
        if (StringUtils.isNotBlank(sensitives) && !sensitives.equals(oldSens.getSensitives())){
            LambdaQueryWrapper<AdSensitive> eq = Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives());
            int count = this.count(eq);
            if (count>0){
                CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"敏感词不能重复");
            }
        }
//        if (StringUtils.isBlank(adSensitive.getSensitives())){
//            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"敏感词不能为空");
//        }
//        //3.2敏感词不能重复
//        if (oldSens.equals(adSensitive.getSensitives())){
//            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"敏感词不能重复");
//        }
        updateById(adSensitive);
        return ResponseResult.okResult();
    }

    /**
     * 敏感词删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        //1.参数校验
        if (id==null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"参数为空");
        }
        //2.检查敏感词是否存在
        AdSensitive adSensitive = this.getById(id);
        if (adSensitive==null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"敏感词不存在");
        }
        //3.执行删除
        removeById(id);
        return ResponseResult.okResult();
    }

    //依赖注入
    @Autowired
    private AdSensitiveMapper adSensitiveMapper;

    /**
     * 查询所有敏感词列表
     * @return
     */
    @Override
    public ResponseResult<List<String>> selectAllSensitives() {
        return ResponseResult.okResult(adSensitiveMapper.findAllSensitives());
    }
}
